<?php

namespace App\Admin\Controllers;

use App\Models\Celue;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CelueController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Celue::with('type'), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('title','标题');
            $grid->column('logo','图片')->image('',50,50);
            $grid->column('jianjie','简介');
            $grid->column('type.name','类型');
            $grid->column('is_display','是否显示')->switch();
            $grid->column('created_at','创建时间');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title','标题');
                $filter->equal('celue_type_id','类型')->select(\App\Models\CelueType::all()->pluck('name','id'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Celue(), function (Show $show) {
            $show->field('id');
            $show->field('detail');
            $show->field('is_display');
            $show->field('jianjie');
            $show->field('logo');
            $show->field('title');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(Celue::with('type'), function (Form $form) {
            $form->display('id');
            $form->text('title','标题')->required();
            $form->radio('celue_type_id','策略类型')->options(\App\Models\CelueType::all()->pluck('name','id'))->required();
            $form->image('logo','缩略图')->autoUpload()->maxSize(102400)->chunkSize(1024)->uniqueName();
            $form->textarea('jianjie','简介')->required();
            $form->editor('detail','详情')->required();
            $form->switch('is_display')->options([
                'on'  => ['value' => 1, 'text' => '显示', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'danger'],
            ])->default(1);
        });
    }
}
